import requests
import threading
import sys
import os
from concurrent.futures import ThreadPoolExecutor

sys.path.append(
    os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
)

from model.park_location_record import ParkRecord
from database.park_location_record_table import ParkRecordTable
from common.logger import logger
import spider_config

lock = threading.RLock()

def get_city_list():
    url = 'https://api.wandacm.com.cn/wdmp/plaza/v1/city/cities'
    return requests.get(url).json()
    
def get_park_list_by_city(city_id: str):
    url = f"https://api.wandacm.com.cn/client/v1/city/parkings?cityId={city_id}"
    headers = {
        "memberId": "0",
    }
    rspJson = requests.get(url, headers=headers).json()
    with lock:
        with ParkRecordTable(spider_config.etcp_park_id) as table:
            for item in rspJson["data"]["parkInfos"]:
                try:
                    record = ParkRecord()
                    record.park_id = item["parkingId"]
                    # record.lat = item["latitude"]
                    # record.lon = item["longitude"]
                    record.park_name = item["parkingName"]
                    record.address = item["address"]
                    record.platform_id = spider_config.etcp_park_id
                    if not table.exist("park_id", record.park_id):
                        table.insert(record)
                        logger.success(f"{record.park_name}({record.address})")
                except Exception as e:
                    logger.exception(e)
            table.commit()
            
if __name__ == '__main__':
    city_list = get_city_list()
    thread_pool = ThreadPoolExecutor(max_workers=100)
    for city in city_list["data"]:
        thread_pool.submit(get_park_list_by_city, city["cityId"])
    thread_pool.shutdown()
    logger.success("done")
    